// 浏览首页
//  MZY_BrowseViewController.m
//  FullHouse
//
//  Created by dllo on 16/1/15.
//  Copyright © 2016年 yy. All rights reserved.
//

#import "MZY_BrowseViewController.h"
#import "MZY_InspirationViewController.h"
#import "NetworkingTool.h"
#import "MZY_HomePageModel.h"
#import "MZY_OnlyPicture.h"
#import "MZY_IntroduceCell.h"
#import "UIImageView+WebCache.h"
#import "MBProgressHUD.h"
#import "MZY_ScrollViewController.h"
#import "MZY_detailViewController.h"
#import "MZY_OrangeViewController.h"
#import "MZY_PurpleViewController.h"
#import "MJRefresh.h"
#import "BannarView.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface MZY_BrowseViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *informationArr;
@property(nonatomic, retain)NSMutableArray *pictureArr;
@property(nonatomic, retain)MBProgressHUD *hud;
@property(nonatomic, retain)BannarView *bannarView;
@property(nonatomic, assign)NSInteger number;

@end

@implementation MZY_BrowseViewController

- (void)dealloc
{
    [self.tableView release];
    [self.informationArr release];
    [self.hud release];
    [self.pictureArr release];
    [self.bannarView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createData];
    [self createView];
}

#pragma mark 获取数据
- (void)createData{
    [NetworkingTool networkingWithStrURL:@"http://api.meilijia.com/app/featured_page3?page=1" type:GETType bodyStr:nil block:^(id result) {
        self.informationArr = [NSMutableArray array];
        for (NSDictionary *temp in result[@"data"][@"featured_items"][@"items"]) {
            MZY_HomePageModel *homePage = [[MZY_HomePageModel alloc]init];
            [homePage setValuesForKeysWithDictionary:temp];
            [self.informationArr addObject:homePage];
            [homePage release];
        }
        self.pictureArr = [NSMutableArray array];
        for (NSDictionary *temp in result[@"data"][@"featured_banners2"][@"banners"]) {
            MZY_HomePageModel *homepage = [[MZY_HomePageModel alloc]init];
            [homepage setValuesForKeysWithDictionary:temp];
            [self.pictureArr addObject:homepage];
            [homepage release];
        }
        self.bannarView.picArr = self.pictureArr;
        [self.tableView reloadData];
        self.hud.hidden = YES;
    }];
}

#pragma mark 加载数据
- (void)loadData{
    NSString *str = [NSString stringWithFormat:@"http://api.meilijia.com/app/featured_page3?page=%ld",self.number ++];
    [NetworkingTool networkingWithStrURL:str type:GETType bodyStr:nil block:^(id result) {
        for (NSDictionary *temp in result[@"data"][@"featured_items"][@"items"]) {
            MZY_HomePageModel *homePage = [[MZY_HomePageModel alloc]init];
            [homePage setValuesForKeysWithDictionary:temp];
            [self.informationArr addObject:homePage];
            [homePage release];
        }
        [self.tableView reloadData];
    }];
}

#pragma mark 创建界面
- (void)createView{
    
    self.view.backgroundColor = [UIColor cyanColor];
    // 自定义标题
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    titleLabel.text = @"浪漫满屋";
    titleLabel.textColor = [UIColor orangeColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont systemFontOfSize:20];
    self.navigationItem.titleView = titleLabel;
    [titleLabel release];
    
    // 轮播图封装
    self.bannarView = [[BannarView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 250)];
    self.bannarView.backgroundColor = [UIColor cyanColor];
    
    // 创建"tableView"
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 10, WIDTH - 20, HEIGHT - 64 - 49 - 20) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor cyanColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    self.tableView.tableHeaderView = self.bannarView;
    [self.tableView registerClass:[MZY_OnlyPicture class] forCellReuseIdentifier:@"onlyPicture"];
    [self.tableView registerClass:[MZY_IntroduceCell class] forCellReuseIdentifier:@"introduce"];
    [self.tableView release];

    // 加载动画
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    self.number = 2;
    // 下拉刷新
    [self.tableView addHeaderWithTarget:self action:@selector(refreshView)];
    self.tableView.headerRefreshingText = @"正在刷新中,请稍后...";
    // 上拉加载
    [self.tableView addFooterWithTarget:self action:@selector(loadViewdata)];
    self.tableView.footerRefreshingText = @"正在加载中,请稍后...";
}

#pragma mark 刷新
- (void)refreshView{
    [self createData];
    [self.tableView headerEndRefreshing];
}

#pragma mark 加载
- (void)loadViewdata{
    [self loadData];
    [self.tableView footerEndRefreshing];
}

#pragma mark "tableView"第一个必须执行的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.informationArr.count;
}

#pragma mark "tableView"第二个必须执行的方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MZY_HomePageModel *homePage = self.informationArr[indexPath.row];
    if ([homePage.type isEqualToString:@"work"]) {
        self.tableView.rowHeight = 270;
        MZY_IntroduceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"introduce" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellEditingStyleNone;
        [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:homePage.cover_pic]];
        [cell.headImageView sd_setImageWithURL:[NSURL URLWithString:homePage.author_face]];
        cell.headImageView.layer.cornerRadius = 30;
        cell.headImageView.layer.masksToBounds = YES;
        cell.titleLabel.text = homePage.title;
        cell.titleLabel.textColor = [UIColor orangeColor];
        cell.introduceLabel.text = homePage.title_sub;
        cell.introduceLabel.font = [UIFont systemFontOfSize:15];
        [cell.likeButton setImage:[UIImage imageNamed:@"zan.png"] forState:UIControlStateNormal];
        [cell.likeButton addTarget:self action:@selector(likeAction:) forControlEvents:UIControlEventTouchUpInside];
        cell.likeCountLabel.text = homePage.liked_count;
        cell.likeCountLabel.textAlignment = NSTextAlignmentCenter;
        NSString *priceStr = homePage.work_design_price;
        cell.priceLabel.text = [NSString stringWithFormat:@"%@%@",priceStr,@"元/平米"];
        cell.priceLabel.textAlignment = NSTextAlignmentCenter;
        cell.priceLabel.alpha = 0.6;
        return cell;
    }else if([homePage.type isEqualToString:@"2"]){
        self.tableView.rowHeight = 200;
        MZY_OnlyPicture *cell = [tableView dequeueReusableCellWithIdentifier:@"onlyPicture" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellEditingStyleNone;
        [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:homePage.pic]];
        cell.titleLabel.text = homePage.title;
        cell.titleLabel.textAlignment = NSTextAlignmentCenter;
        cell.titleLabel.textColor = [UIColor whiteColor];
        cell.titleLabel.font = [UIFont systemFontOfSize:25];
        cell.introduceLabel.text = [NSString stringWithFormat:@"%@%@",homePage.col_count,homePage.col_count_text];
        cell.introduceLabel.textAlignment = NSTextAlignmentCenter;
        cell.introduceLabel.textColor = [UIColor whiteColor];
        cell.introduceLabel.backgroundColor = [UIColor purpleColor];
        cell.introduceLabel.alpha = 0.8;
        return cell;
    }else if ([homePage.type isEqualToString:@"1"]){
        self.tableView.rowHeight = 200;
        MZY_OnlyPicture *cell = [tableView dequeueReusableCellWithIdentifier:@"onlyPicture" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellEditingStyleNone;
        [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:homePage.pic]];
        cell.titleLabel.text = homePage.title;
        cell.titleLabel.textAlignment = NSTextAlignmentCenter;
        cell.titleLabel.textColor = [UIColor whiteColor];
        cell.titleLabel.font = [UIFont systemFontOfSize:25];
        cell.introduceLabel.text = [NSString stringWithFormat:@"%@%@",homePage.col_count,homePage.col_count_text];
        cell.introduceLabel.textAlignment = NSTextAlignmentCenter;
        cell.introduceLabel.textColor = [UIColor whiteColor];
        cell.introduceLabel.backgroundColor = [UIColor orangeColor];
        cell.introduceLabel.alpha = 0.8;
        return cell;
    }else{
        self.tableView.rowHeight = 200;
        MZY_OnlyPicture *cell = [tableView dequeueReusableCellWithIdentifier:@"onlyPicture" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellEditingStyleNone;
        [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:homePage.pic]];
        cell.titleLabel.text = homePage.title;
        cell.titleLabel.textAlignment = NSTextAlignmentCenter;
        cell.titleLabel.textColor = [UIColor whiteColor];
        cell.titleLabel.font = [UIFont systemFontOfSize:25];
        cell.introduceLabel.text = homePage.descr;
        cell.introduceLabel.textAlignment = NSTextAlignmentCenter;
        cell.introduceLabel.textColor = [UIColor whiteColor];
        cell.introduceLabel.backgroundColor = [UIColor greenColor];
        cell.introduceLabel.alpha = 0.8;
        return cell;
    }
}

#pragma mark "collectionView"的第一个必须执行的方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.pictureArr.count;
}

#pragma mark "collectionView"的第一个必须执行的方法
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collection" forIndexPath:indexPath];
    return cell;
}

#pragma mark 喜欢"button"的点击方法
- (void)likeAction:(UIButton *)button{
    
}

#pragma mark "collectionView"的点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    MZY_ScrollViewController *scrollVC = [[MZY_ScrollViewController alloc]init];
    scrollVC.uri = [self.pictureArr[indexPath.row]uri];
    [self.navigationController pushViewController:scrollVC animated:YES];
    [scrollVC release];
}

#pragma mark "tableView"的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MZY_HomePageModel *home = self.informationArr[indexPath.row];
    if ([home.type isEqualToString:@"work"]) {
        self.hidesBottomBarWhenPushed = YES;
        MZY_detailViewController *detailVC = [[MZY_detailViewController alloc]init];
        [self.navigationController pushViewController:detailVC animated:YES];
        detailVC.idStr = [self.informationArr[indexPath.row]ID];
        self.hidesBottomBarWhenPushed = NO;
    }else if ([home.type isEqualToString:@"1"]){
        self.hidesBottomBarWhenPushed = YES;
        MZY_OrangeViewController *orangeVC = [[MZY_OrangeViewController alloc]init];
        [self.navigationController pushViewController:orangeVC animated:YES];
        orangeVC.idStr = [self.informationArr[indexPath.row]ID];
        orangeVC.titleStr = home.title;
        self.hidesBottomBarWhenPushed = NO;
    }else if([home.type isEqualToString:@"2"]){
        self.hidesBottomBarWhenPushed = YES;
        MZY_PurpleViewController *purpleVC = [[MZY_PurpleViewController alloc]init];
        [self.navigationController pushViewController:purpleVC animated:YES];
        purpleVC.idStr = [self.informationArr[indexPath.row]ID];
        purpleVC.titleStr = home.title;
        self.hidesBottomBarWhenPushed = NO;
    }else{
        MZY_InspirationViewController *insVC = [[MZY_InspirationViewController alloc]init];
        [self.navigationController pushViewController:insVC animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
